var BASE_URL = 'http://jinzhidata.com/v2'

function formatTime(timeVal, fmt) {
    timeVal = new Date(timeVal.replace(/^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}).*$/, '$1 $2 GMT+0000').replace(/-/g, '/'))
    var o = {
        "M+": timeVal.getMonth() + 1, //月份
        "d+": timeVal.getDate(), //日
        "h+": timeVal.getHours(), //小时
        "m+": timeVal.getMinutes(), //分
        "s+": timeVal.getSeconds(), //秒
        "q+": Math.floor((timeVal.getMonth() + 3) / 3), //季度
        "S": timeVal.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (timeVal.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}


$(function () {
    $.ajax({
        url: BASE_URL + "/carouselFigure/list",
        type: "get",
        contentType: 'application/json',
        dataType: "json",
        async: false,
        success: function (response) {
            var html = ''
            response.data.forEach(function (item, index) {
                html += '<li class="swiper-slide">'
                html += '   <a href="http://jinzhidata.com/v2/article.html?articleId=' + item.articleId + '">'
                html += '       <img src="' + item.carouselFigureImage + '"/>'
                html += '        <div class="slider-desc">' + item.articleTitle + '</div>'
                html += '   </a>'
                html += '</li>'
            })
            $(".swiper-list").html(html)
        }
    })

    $(".shop-search").click(function () {
        var searchWord = $('#shop-input').val()
        window.location.href = 'resource_list.html?searchWord=' + searchWord
        $.ajax({
            url: BASE_URL + "/resource/list/like",
            data: {
                resourceName: searchWord,
                page: 0,
                size: 999999
            },
            type: "get",
            contentType: 'application/json',
            dataType: "json",
            success: function (response) {
            }
        })
    })
    new Swiper('.banner', {
        pagination: '.sp01',
        paginationClickable: true,
        autoplay: "3000",
        loop: "ture",
        speed: 500,
        autoplayDisableOnInteraction: false
    });

    new Swiper('.nav_iconbox', {
        speed: 500,
        autoplayDisableOnInteraction: false,
    });

    var n = 0;
    //监听滚动条事件，改变背景透明度
    $(window).scroll(function () {
        var top = $(document).scrollTop();
        if ((top / 150) >= 0.9) {
            n = 0.9;
        } else {
            n = (top / 150);
        }
        $(".search-box-cover").css("opacity", n);

        // 搜索框背景色和搜索图标
        if (n > 0.4) {
            $(".search-box").css("background", "#ffffff");
            $(".shop-search").attr("src", "./images/icon_search.png")
        } else {
            $(".search-box").css("background", "rgba(255,255,255,0)");
            $(".shop-search").attr("src", "./images/icon_search_w.png")
        }

    });

    var timer;

    //滚动文字
    function runtxt() {
        $(".scrollnews ul").animate({marginTop: "-.4rem"}, 300,
            function () {
                $(".scrollnews ul li:last").after($(".scrollnews ul li:first"))
                $(".scrollnews ul").css("margin-top", 0)
            }
        )
    }

    $(".scrollnews ul li a").mouseenter(
        function () {
            clearInterval(timer)
        }
    )
    $(".scrollnews ul li a").mouseleave(
        function () {
            timer = setInterval(runtxt, 4000)
        }
    )
    timer = setInterval(runtxt, 4000);

    // todo 首页显示全部
    $.ajax({
        url: BASE_URL + "/article/list?page=0&size=9999",
        type: "get",
        contentType: 'application/json',
        dataType: "json",
        success: function (response) {
            var data = response.data
            var html = '';
            for (var i = 0; i < data.length; i++) {
                html += '<li>';
                html += '<a href="' + BASE_URL + '/article.html?articleId=' + data[i].articleId + '" >';
                html += '<h2>';
                html += '<p>' + data[i].articleTitle + '</p>';
                html += '<span>' + formatTime(data[i].createTime, "yyyy-MM-dd") + '</span>';
                html += '</h2>';
                html += '<i><img src="' + data[i].articleCover + '"></i>';
                html += "</a>";
                html += "</li>";
            }
            $(".newslist>ul").html(html)
        }
    })


    var url = location.href.toString();//url不能写死
    $.ajax({
        type: "get",
        url: BASE_URL + "/share/getShareConfig",
        dataType: "json",
        async: false,
        data: {'url': url},
        success: function (res) {
            wx.config({
                debug: false,////生产环境需要关闭debug模式
                appId: res.data.appid,//appId通过微信服务号后台查看
                timestamp: res.data.timestamp,//生成签名的时间戳
                nonceStr: res.data.nonceStr,//生成签名的随机字符串
                signature: res.data.signature,//签名
                jsApiList: [//需要调用的JS接口列表
                    'checkJsApi',//判断当前客户端版本是否支持指定JS接口
                    'onMenuShareTimeline',//分享给好友
                    'onMenuShareAppMessage'//分享到朋友圈
                ]
            });
        },
        error: function (xhr, status, error) {
            alert(status);
            //alert(xhr.responseText);
        }
    })

    wx.ready(function () {
        var link = window.location.href;
        var protocol = window.location.protocol;
        var host = window.location.host;
        //分享朋友圈
        wx.onMenuShareTimeline({
            title: '尽智研究院-影响100万交通企业',
            link: link,
            imgUrl: BASE_URL + '/images/logo.png', // 自定义图标
            success: function (res) {
                //alert('shared success');
                //some thing you should do
            }
        });
        //分享给好友
        wx.onMenuShareAppMessage({
            title: '尽智研究院-影响100万交通企业', // 分享标题
            desc: '交通行业项目合作、资源干货、前沿技术、招聘信息', // 分享描述
            link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: BASE_URL + '/images/logo.png', // 自定义图标
            success: function () {
                // 用户确认分享后执行的回调函数
            }
        });
        wx.error(function (res) {
            alert(res.errMsg);
        });
    });
})

